using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;

namespace RabbitsWare.Business.TwitterApollo.BusinessEntities
{
    public class CompanyOperator
    {
        int m_companyId;

        public int CompanyId
        {
            get { return m_companyId; }
            set { m_companyId = value; }
        }

        bool m_isBusy;

        public bool IsBusy
        {
            get { return m_isBusy; }
            set { m_isBusy = value; }
        }

        int m_id;

        public int Id
        {
            get { return m_id; }
            set { m_id = value; }
        }

        string m_name;

        public string Name
        {
            get { return m_name; }
            set { m_name = value; }
        }

        private CompanyOperator(int companyId, int id, string name, bool isBusy)
        {
            m_companyId = companyId;
            m_id = id;
            m_name = name;
            m_isBusy = isBusy;
        }

        internal void MarkAsBusy()
        {
            Database database = GetDatabase();
            DbCommand storedProcCommand = database.GetStoredProcCommand("[dbo].[TweetApolloMarkCompanyOperatorBusy]");
            database.AddInParameter(storedProcCommand, "@operatorId", DbType.Int32, m_id);
            database.ExecuteReader(storedProcCommand);            
        }

        private static Database GetDatabase()
        {
            return DatabaseFactory.CreateDatabase("RabbitsWare");
        }

        internal void MarkAsAvaiable()
        {
            Database database = GetDatabase();
            DbCommand storedProcCommand = database.GetStoredProcCommand("[dbo].[TweetApolloMarkCompanyOperatorAvaiable]");
            database.AddInParameter(storedProcCommand, "@operatorId", DbType.Int32, m_id);
            database.ExecuteReader(storedProcCommand);            
        }

        internal static CompanyOperator CreateCompanyOperator(string companyOperatorName)
        {
            Database database = GetDatabase();
            DbCommand storedProcCommand = database.GetStoredProcCommand("[dbo].[TweetApolloGetCompanyOperator]");
            database.AddInParameter(storedProcCommand, "@operatorName", DbType.String, companyOperatorName);

            CompanyOperator companyOperator = null;

            using (IDataReader reader = database.ExecuteReader(storedProcCommand))
            {
                while (reader.Read())
                {
                    companyOperator = new CompanyOperator(reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), reader.GetBoolean(3));
                }
            }

            return companyOperator;
        }

        public static CompanyOperator GetAvaiableCompanyOperator(int companyId)
        {
            Database database = GetDatabase();
            DbCommand storedProcCommand = database.GetStoredProcCommand("[dbo].[TweetApolloGetAvaiableCompanyOperator]");
            database.AddInParameter(storedProcCommand, "@companyId", DbType.Int32, companyId);

            CompanyOperator companyOp = null;

            using (IDataReader reader = database.ExecuteReader(storedProcCommand))
            {
                while (reader.Read())
                {
                    companyOp = new CompanyOperator(reader.GetInt32(0), reader.GetInt32(1), reader.GetString(2), reader.GetBoolean(3));
                    companyOp.MarkAsBusy();
                }
            }

            return companyOp;
        }
    }
}
